﻿/*
 *	(*^。^*) TFrameWork License (*^。^*)
 */

/*
 *  Title :
 *
 *  Description :
 *
 *
 *  Date :
 *
 *  Author : DengPing
 *
 *  Modify :
 */
using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;

namespace TFrameWork.Module.ActionEditor
{
    [CreateAssetMenu(menuName = "ActionEditor/Create Data", fileName = "ActionData")]
    public class ActionData : DataBase
    {
        public string defaultPrePrefab;
        public string SkillName;
        public int FrameCount = 100;
        public int FrameRote = 30;

        public List<TrackData> trackDataList = new List<TrackData>();

        public TrackData GetTrackList<T>() where T : TrackEventBase, new()
        {
            var attr = typeof(T).GetCustomAttribute(typeof(TrackDataKeyAttribute)) as TrackDataKeyAttribute;
            if (attr == null)
            {
                ActionEditorLog.Error(typeof(T).Name + " 该类型数据没有标记 TrackDataKeyAttribute 标签");
                return null;
            }

            for (int i = 0; i < trackDataList.Count; i++)
            {
                if (trackDataList[i].trackDataKey == attr.key)
                {
                    return trackDataList[i];
                }
            }

            TrackData data = ScriptableObject.CreateInstance<TrackData>();
            data.trackDataKey = attr.key;
            data.name = attr.key + "_Track";
            trackDataList.Add(data);
#if UNITY_EDITOR
            UnityEditor.AssetDatabase.AddObjectToAsset(data, this);
            UnityEditor.EditorUtility.SetDirty(this);
            UnityEditor.AssetDatabase.SaveAssets();
#endif

            return data;
        }

        public void Sort()
        {
            for (int i = 0; i < trackDataList.Count; i++)
            {
                trackDataList[i].Sort();
            }
        }


#if UNITY_EDITOR
        [NonSerialized] private static Action _actionDataChanged;

        /// <summary>
        /// 编辑器注册数据发送改变的函数
        /// </summary>
        /// <param name="action"></param>
        public static void SetValidateAction(Action action)
        {
            _actionDataChanged = action;
        }

        private void OnValidate()
        {
            _actionDataChanged?.Invoke();
        }
#endif
    }
}
